/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.openmrs.module.basicmodule.DSS.Interpreter;

import org.openmrs.module.basicmodule.DSS.ast.AST;

/**
 *
 * @author Stephen
 */
public class ExponentOperator {
    private AST t;
    private InterpreterVisitor iv;

    public ExponentOperator(AST t, InterpreterVisitor iv) {
        this.t = t;
        this.iv = iv;
    }

    public Object execute() {
        Object operand1 = t.getKid(1).accept(iv);
        Object operand2 = t.getKid(2).accept(iv);
        String type = InterpreterVisitor.parseType((String) operand1);

        if (type.equalsIgnoreCase("float")) {
            Float op1 = Float.parseFloat((String)operand1);
            Float op2 = Float.parseFloat((String)operand2);
            float result = (float)Math.pow(op1, op2);
            return Float.toString(result);
        } else if (type.equalsIgnoreCase("int")) {
            int op1 = Integer.parseInt((String)operand1);
            int op2 = Integer.parseInt((String)operand2);
            int result = (int)Math.pow(op1, op2);
            return Integer.toString(result);
        }
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
